﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using System.Data;
using V5_Model;

namespace V5_DAL
{
    public class DALFriend
    {
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "K_Friend");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_Friend");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelFriend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_Friend(");
            strSql.Append("ID,FriendTitle,FriendUrl,FriendLogo,FriendReadme,IsShow)");
            strSql.Append(" values (");
            strSql.Append("@ID,@FriendTitle,@FriendUrl,@FriendLogo,@FriendReadme,@IsShow)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@FriendTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@FriendUrl", SqlDbType.NVarChar,100),
					new SqlParameter("@FriendLogo", SqlDbType.NVarChar,100),
					new SqlParameter("@FriendReadme", SqlDbType.NVarChar,255),
					new SqlParameter("@IsShow", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.FriendTitle;
            parameters[2].Value = model.FriendUrl;
            parameters[3].Value = model.FriendLogo;
            parameters[4].Value = model.FriendReadme;
            parameters[5].Value = model.IsShow;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelFriend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_Friend set ");
            strSql.Append("FriendTitle=@FriendTitle,");
            strSql.Append("FriendUrl=@FriendUrl,");
            strSql.Append("FriendLogo=@FriendLogo,");
            strSql.Append("FriendReadme=@FriendReadme,");
            strSql.Append("IsShow=@IsShow");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@FriendTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@FriendUrl", SqlDbType.NVarChar,100),
					new SqlParameter("@FriendLogo", SqlDbType.NVarChar,100),
					new SqlParameter("@FriendReadme", SqlDbType.NVarChar,255),
					new SqlParameter("@IsShow", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.FriendTitle;
            parameters[2].Value = model.FriendUrl;
            parameters[3].Value = model.FriendLogo;
            parameters[4].Value = model.FriendReadme;
            parameters[5].Value = model.IsShow;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Friend ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelFriend GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,FriendTitle,FriendUrl,FriendLogo,FriendReadme,IsShow from K_Friend ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            ModelFriend model = new ModelFriend();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.FriendTitle = ds.Tables[0].Rows[0]["FriendTitle"].ToString();
                model.FriendUrl = ds.Tables[0].Rows[0]["FriendUrl"].ToString();
                model.FriendLogo = ds.Tables[0].Rows[0]["FriendLogo"].ToString();
                model.FriendReadme = ds.Tables[0].Rows[0]["FriendReadme"].ToString();
                if (ds.Tables[0].Rows[0]["IsShow"].ToString() != "")
                {
                    model.IsShow = int.Parse(ds.Tables[0].Rows[0]["IsShow"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,FriendTitle,FriendUrl,FriendLogo,FriendReadme,IsShow ");
            strSql.Append(" FROM K_Friend ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,FriendTitle,FriendUrl,FriendLogo,FriendReadme,IsShow ");
            strSql.Append(" FROM K_Friend ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_Friend";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        #endregion  成员方法
    }
}
